<?php
/*
Template Name: Data Entry
Author: NajeebMedia.com
*/
?>
<?php get_header(); ?>

<div id="content">

<?php
//error_reporting(1);
/*getting products
topMenuCats are defined in template-topmenu.php, so same products categories are being used here
*/

function insert_attachment($file_handler, $post_id, $setthumb='false') {
		require_once(ABSPATH . "wp-admin" . '/includes/image.php');
		require_once(ABSPATH . "wp-admin" . '/includes/file.php');
		require_once(ABSPATH . "wp-admin" . '/includes/media.php');

		$attach_id = media_handle_upload( $file_handler, $post_id );

		if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
		return $attach_id;
}

if(isset($_POST['post'])){
	/*print "<pre>";
	print_r($_POST);
	print_r($_FILES);
	print "</pre>";
	exit;*/
	
	//////// adding post data
	$my_post = array(
		 'post_title' => $_POST['cname'],
		 'post_content' =>  '',
		 'post_status' => 'publish',
		 'post_author' => 1,
		 'post_category' => array(12, 446)		//446 = Data Entry
		 										//12  = Companies
	  );
	 $pID=wp_insert_post( $my_post );	
	  
	///////// adding meta Information
	add_post_meta($pID, 'emailAddress', $_POST['email']);
	add_post_meta($pID, 'phoneNumber', $_POST['phone']);
	add_post_meta($pID, 'contactPerson', $_POST['cPerson']);
	add_post_meta($pID, 'siteURL', $_POST['siteUrl']);
	//add_post_meta($pID, 'youTubeVideo', $_POST['video']);
	add_post_meta($pID, 'address', $_POST['address']);
	add_post_meta($pID, 'unlisted_products', $_POST['unlisted']);		//products which are not listed in site but
																		//company have
	
																				

	///////////// adding attachment
	if($_FILES['logo']['error']==0){
		insert_attachment('logo', $pID, true);
	}
	
	echo "<h1>Data save</h1>";
	//exit;
}
?>

<script language="javascript" type="text/javascript">
function validateNewCompnay(){
	if(jQuery('#cname').val()==""){
		alert("Please enter company name");
		jQuery('#cname').focus();
		return false;
	}

	if(jQuery('#detail').val()==""){
		alert("Please enter company detail");
		jQuery('#detail').focus();
		return false;
	}

	if(!validateEmail(jQuery("#email").val())){
		alert("Please enter Comany Email Address");
		jQuery("#email").focus();
		return false;
	}

	if(jQuery("#phone").val()==""){
		alert("Please enter Company Phone Number");
		jQuery("#phone").focus();
		return false;
	}

	if(jQuery('#cPerson').val()==""){
		alert("Please enter company Contact Person Name");
		jQuery('#cPerson').focus();
		return false;
	}


	if(!is_valid_url(jQuery("#siteUrl").val())){
		alert("Please enter Comany Website Address");
		jQuery("#siteUrl").focus();
		return false;
	}

	return true;
}

function is_valid_url(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
 }  


</script>

<div class="custom-form">

<form action="http://stoneselector.com/data-entry-form/" method="post" enctype="multipart/form-data">
<ul>
	<li>
    <span class="span">Company Name:</span>
	<input type="text" placeholder="Company Name" class="field-text" name="cname" id="cname" />
    </li>
    
	<li>
    <span class="span">Email:</span>
	<input type="text" placeholder="Email" class="field-text" name="email" id="email" />
    </li>

	<li>
    <span class="span">Phone:</span>
	<input type="text" placeholder="Phone Number" class="field-text" name="phone" />
    </li>
	<li>
	  <span class="span">Site URL:</span>
	  <input type="text" placeholder="Site URL" class="field-text" name="siteUrl" id="siteUrl" />
	  </li>
	<!--<li>
    <span class="span">Video:</span>
	<input type="text" placeholder="Youtube Video" class="field-text" name="video" id="video" />
    </li>-->
	<li>
    <span class="span">Company Logo:</span>
	<input type="file" placeholder="Logo" class="field-text" name="logo" />
    </li>
    <li>
	    <span class="span">Address:</span>
		<textarea cols="30" rows="10" class="field-text" name="address" id="address"></textarea>
    </li>
    <li class="form-button">
      <input name="post" type="submit" class="field-button" onclick="return validateNewCompnay()" value="Save Data" />
    </li>
</ul>

</form>

</div>

</div><!-- #content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>
